﻿var f_createwidget;
var jQuery;
var byhanddialog;
var tempId = "0";
var refId = 0;
function byhand_c_widget($) {
    $.widget("custom.byhandwidget", {
        // default options
        options: {
            payment: function (e, data) {
                var size = {
                    width: window.innerWidth || document.body.clientWidth,
                    height: window.innerHeight || document.body.clientHeight
                }
                if (tempId != "") {
                    if (size.width <= 600) {
                        window.location = 'http://byhand.vn/Payment/WidgetPopup?location=' + top.location.href + '&opentype=2&id=' + data.id + '&refid=' + refId + '&tempID=' + tempId;
                        return;
                    }
                    var container_payment;
                    container_payment = $('.byhand-container-frame[data-id=' + data.id + ']');
                    if (container_payment.length === 0) {

                        $('body').append('<div class="modal fade byhand-container-frame" data-id="' + data.id + '" id="checkout" aria-hidden="true"><div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> <h4 class="modal-title">Hoàn tất đơn đặt hàng</h4> </div><div class="byhand-wrapper-widgetpopup"></div></div> </div> </div>');
                        //$('body').append('<div class="byhand-container-frame" data-id="' + data.id + '" style="height: auto; width: auto;"></div>');
                        container_payment = $('.byhand-container-frame[data-id=' + data.id + ']');
                        $(container_payment).find('.byhand-wrapper-widgetpopup').html('<iframe src="http://byhand.vn/Payment/WidgetPopup?location=' + top.location.href + '&opentype=2&id=' + data.id + '&refid=' + refId + '&tempID=' + tempId + '" width="578" height="570" frameborder="0" scrolling="no" id="the_iframe"></iframe>');
                        //$(container_payment).dialog({ width: 750, height: 800, closeOnEscape: true, modal: true, resizable: false });
                        $(container_payment).modal({ keyboard: true });
                    } else {
                        var content = $(container_payment);
                        //$(content).dialog();
                        $(content).modal({ keyboard: true });
                    }
                }
            },
            viewmore: function (e, data) {
                var win = window.open("http://byhand.vn/home/index", '_blank');
                win.focus();
            }
        },

        // the constructor
        _create: function () {

            var widget = this;
            var name = $(this.element).attr('data-name');
            var width = $(this.element).attr('data-width');
            refId = $(this.element).attr('data-refid');
            var id = $(this.element).attr('data-id');
            var that = this.element;
            tempId = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
                var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
                return v.toString(16);

            });
            $(that).find('.byhand-wrapper').remove();
            $(that).append('<div class="byhand-wrapper" style="border: 1px solid #97261E;-webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; background-color: #fff;width:' + width + 'px;"> <div style="width:100%;height:' + (width * 0.75) + 'px"> <iframe src="http://byhand.vn/payment/widgetembed?id=' + id + '" style="width:100%;height:100%;" frameborder="0" scrolling="no"></iframe> </div> <div class="byhand-action" style="padding:10px;"> <a class="byhand-viewmore-btn btn btn-default pull-right">More</a> <a class="byhand-payment-btn btn btn-default pull-right">Payment</a><div class="clearfix"></div> </div> </div>');
            $(that).find('.byhand-viewmore-btn').click(function () {
                widget._trigger("viewmore", null, { id: id });
            });
            $(that).find('.byhand-payment-btn').click(function () {
                widget._trigger("payment", null, { id: id });
            });
        },
        // events bound via _on are removed automatically
        // revert other modifications here
        _destroy: function () {

        }
    });
}
function byhand_loadJquery(afterloaded, finalexec) {
    // Localize jQuery variable
    /******** Load jQuery if not present *********/
    if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.10.2') {
        var script_tag = document.createElement('script');
        script_tag.setAttribute("type", "text/javascript");
        script_tag.setAttribute("src",
            "http://byhand.vn/content/js/jquery.min.js");

        if (script_tag.readyState) {
            script_tag.onreadystatechange = function () { // For old versions of IE
                if (this.readyState == 'complete' || this.readyState == 'loaded') {
                    scriptLoadHandler(jQuery);
                }
            };
        } else {
            script_tag.onload = scriptLoadHandler;
        }
        // Try to find the head, otherwise default to the documentElement
        (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
    } else {
        // The jQuery version on the window is the one we want to use
        jQuery = window.jQuery;
        afterloaded(jQuery, finalexec);
    }
    /******** Called once jQuery has loaded ******/
    function scriptLoadHandler() {
        // Restore $ and window.jQuery to their previous values and store the
        // new jQuery in our local jQuery variable
        jQuery = window.jQuery;
        // Call our main function
        afterloaded(jQuery, finalexec);
    }
}
function byhand_loadJqueryui(jQuery, afterloaded) {
    // Localize jQuery variable
    /******** Load jQuery if not present *********/
    if ((jQuery.ui === undefined || jQuery.ui.version !== '1.10.3') && !jQuery("script[src='http://byhand.vn/content/js/jquery-ui-1.10.3.customwidget.min.js']").length) {
        var script_tag = document.createElement('script');
        script_tag.setAttribute("type", "text/javascript");
        script_tag.setAttribute("src",
            "http://byhand.vn/content/js/jquery-ui-1.10.3.customwidget.min.js");
        if (script_tag.readyState) {
            script_tag.onreadystatechange = function () { // For old versions of IE
                if (this.readyState == 'complete' || this.readyState == 'loaded') {
                    scriptLoadHandler();
                }
            };
        } else {
            script_tag.onload = scriptLoadHandler;
        }
        // Try to find the head, otherwise default to the documentElement
        (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
    } else {
        afterloaded();
    }
    /******** Called once jQuery has loaded ******/
    function scriptLoadHandler() {
        // Call our main function
        afterloaded();
    }
}
function byhand_loadcss($, url) {
    var css_link = $("<link>", {
        rel: "stylesheet",
        type: "text/css",
        href: url
    });
    css_link.appendTo('head');
}
function byhand_finalexec() {

    jQuery(document).ready(function ($) {
        if (!$("link[href='http://byhand.vn/content/css/bootstrapbuttonembed.min.css']").length) {
            (function (d, t) {
                t = d.createElement('link');
                t.setAttribute('href', 'http://byhand.vn/content/css/bootstrapbuttonembed.min.css');
                t.setAttribute('rel', 'stylesheet');
                d.getElementsByTagName('head')[0].appendChild(t)
            })(document)
        }
        if (!$("script[src='http://byhand.vn/content/js/bootstrap.min.js']").length) {
            (function (d, t) { t = d.createElement('script'); t.setAttribute('src', 'http://byhand.vn/content/js/bootstrapbuttonembed.min.js'); d.getElementsByTagName('head')[0].appendChild(t) })(document)
        }
        byhand_c_widget($);
        $('.byhand-payment').byhandwidget();
    });
}
(function () {
    byhand_loadJquery(byhand_loadJqueryui, byhand_finalexec);
})();